﻿using DirectSell.Common;
using DirectSell.Common.Cookie;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectSell.Common.Session
{
    public class SessionInfo
    {

        #region 判断登录状态--郭文昊--2018年10月29日10:08:13
        /// <summary>
        /// 是否登录
        /// </summary>  
        public static bool IsUserLogin
        {
            get { return GetSession() != null ? true : false; }
        }
        #endregion


        #region 判断后台登录状态--郭文昊--2018年10月29日10:08:13
        /// <summary>
        /// 判断后台是否登录
        /// </summary>  
        public static bool IsManagerLogin
        {
            get { return GetManagerSession() != null ? true : false; }
        }
        #endregion

        #region 获取sessionID private
        /// <summary>
        /// 获得sessionID(用户ID)
        /// useriD cookiesID
        /// </summary>
        /// <returns></returns>
        public static string GetSessionID()
        {
            return CookieInfo.GetLoginCookie();
        }
        #endregion

        #region Session存
        /// <summary>
        /// 存储用户信息
        /// </summary>
        public static void SetSession(SessionModel model)
        {
            var _sessionid = model.UserId.ToString();
            CookieInfo.SetBackLoginCookie(_sessionid);
            SessionHelper.SetSession(_sessionid, model);
            SessionHelper.SessionTime(800);
        }
        #endregion

        #region Session存 后台
        /// <summary>
        /// Session存 后台
        /// </summary>
        /// <param name="model"></param>
        public static void SetManagerSession(ManagerSessionModel model)
        {
            var _sessionid = model.ManagerCode.ToString();
            CookieInfo.SetManagerCookie(_sessionid);
            SessionHelper.SetSession(_sessionid, model);
            SessionHelper.SessionTime(800);
        }
        #endregion

        #region Session取--郭文昊--2018年10月29日02:39:39
        /// <summary>
        /// Session 取
        /// </summary>
        /// <returns></returns>
        public static SessionModel GetSession()
        {
            string useriD = CookieInfo.GetBackLoginCookie();
            return SessionHelper.GetSession(useriD) as SessionModel;
        }

        /// <summary>
        /// Session 取 后台
        /// </summary>
        /// <returns></returns>
        public static ManagerSessionModel GetManagerSession()
        {
            string useriD = CookieInfo.GetManagerCookie();
            return SessionHelper.GetSession(useriD) as ManagerSessionModel;
        }
        #endregion

        #region 解析cookie信息--yfx
        /// <summary>
        /// 解析cookie信息
        /// </summary>
        /// <param name="cookievalue">cookie</param>
        /// <returns>返回解析后的cookie</returns>
        private static string AnalysisCookie(string cookievalue)
        {
            if (string.IsNullOrEmpty(cookievalue)) //判断Cookie是否为空
            {
                return null;
            }
            var cookie = JsonConvert.DeserializeObject<CookieModel>(DESncrypt.Decrypt(cookievalue));//获取cookie值
            return (cookie != null) ? cookie.UI_ID : string.Empty;
        }
        #endregion

        #region 获取cookie(后台)--qzz
        /// <summary>
        /// 获取登录Cookie
        /// </summary>
        public static string GetBackLoginCookie()
        {
            var cookievalue = CookieHelper.GetCookieValue("MyCookie");
            return AnalysisCookie(cookievalue);
        }
        #endregion

    }
}
